<?php
/**
 * BQue Description
 * @package core.lib
 * @name Output
 * @filesource Output.php
 * @author Meta Sanjaya
 */

class Output extends Registry
{
    private $output;
    private $template;
    private $headers;
    private $widgets;

    /**
     * @access public
     * @method __construct
     * @param void
     * @return void
     */
    public function __construct()
    {
        $this->output = '';
        $this->template = $this->config->get('default_template');
        $this->widgets = array();
        $this->headers = array();
        ob_start();
    }

    public function renderOutput($vars, $return=false)
    {
        extract($vars);

        include_once

        $this->applyTemplate(ob_get_contents());

        if(!!$return)
        {
            return $this->getOutput();
        }
        else
        {
            foreach($this->headers as $name => $header)
            {
                header($header, true);
            }
            
            echo $this->getOutput();
        }
    }

    public function addWidget($name, $view)
    {
        $this->widgets[$name] = $view;
    }

    public function addHeader($name, $stringHeader, $replace=true)
    {
       if(isset($this->headers[$name]) && !!$replace)
       {
           $this->headers[$name] = $stringHeader;
       }
    }

    public function applyTemplate($xhtml)
    {
        $xml = new DOMDocument;
        $xml->loadHTML($xhtml);
        
        $xsl = new DOMDocument;
        $xsl->load($this->template);

        $process = new XSLTProcessor;
        $process->registerPHPFunctions();
        $process->importStylesheet($xsl);

        $this->output = $process->transformToXml($xml);
    }

    public function getOutput()
    {
        return $this->output;
    }

    public static function display()
    {
        Register::get('view')->renderOutput(array());
    }
}